package pl.model.domain;

import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.search.annotations.Fields;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;

import pl.common.PaddedIntegerBridge;

@Entity
@Indexed
public class Book extends AbstractDBObject {

	@Fields({
		@Field(store=Store.YES, index=Index.TOKENIZED),
		@Field(name="title_forSort", index=Index.UN_TOKENIZED)
	})
	protected String title;
	@ManyToOne(cascade={CascadeType.PERSIST}, fetch=FetchType.LAZY)
	@LazyToOne(LazyToOneOption.NO_PROXY)
	@IndexedEmbedded
	protected Author author; // *..1
	@ManyToOne
	protected Publisher publisher; // *..1
	@ManyToMany(mappedBy="cobooks")
	protected Collection<Author> coauthors; // *..*
	@Field(store=Store.YES)
	protected String tags; // categories/keywords
	
	@Field(index=Index.UN_TOKENIZED)
	@FieldBridge(impl=PaddedIntegerBridge.class)
	protected Integer rank;
	@OneToMany(mappedBy="book")
	protected Collection<Comment> comments;
	
	
	@Fields({
		@Field(store=Store.NO),
		@Field(name="description_forSort", index=Index.UN_TOKENIZED)
	})
	protected String description;

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Book(String title, String tags, Integer rank, String description) {
		super();
		this.title = title;
		this.tags = tags;
		this.rank = rank;
		this.description = description;
	}

	@Override
	public String toString() {
		return super.toString() + " title: " + title + " rank: " + rank + (author!=null?" "+author:" ") + " tags: " + tags  + " description: " + description;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Author getAuthor() {
		return author;
	}

	public void setAuthor(Author author) {
		this.author = author;
	}

	public Publisher getPublisher() {
		return publisher;
	}

	public void setPublisher(Publisher publisher) {
		this.publisher = publisher;
	}

	public Collection<Author> getCoauthors() {
		return coauthors;
	}

	public void setCoauthors(Collection<Author> coauthors) {
		this.coauthors = coauthors;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public Collection<Comment> getComments() {
		return comments;
	}

	public void setComments(Collection<Comment> comments) {
		this.comments = comments;
	}

	public Book(String title, Author author, Publisher publisher,
			Collection<Author> coauthors, String tags,
			Collection<Comment> comments) {
		super();
		this.title = title;
		this.author = author;
		this.publisher = publisher;
		this.coauthors = coauthors;
		this.tags = tags;
		this.comments = comments;
	}

	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Book(String title, String tags) {
		super();
		this.title = title;
		this.tags = tags;
	}

	public Integer getRank() {
		return rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}
	
	
}
